package com.sap.apitest2.deprecated.client;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import com.sap.apitest2.deprecated.service.IService;


public class ServerTest extends AbstractTest implements IService{
    public static void main(String args[]) throws Exception {
    	//testRequestJsonPutServer();
    	testRequestJsonGetMetadata();
    	//testRequestJsonRemoveServer();
    	/*
    	testRequestJsonGetServers();
    	testRequestJsonGetServer();
    	testRequestJsonPutServer();
    	testRequestJsonRemoveServer();
    	*/
        System.out.println("\n"); 
        System.exit(0);
    }
	private static void testRequestJsonGetMetadata()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get servers info, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_METADATA);
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
	private static void testRequestJsonGetServers()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get servers info, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_SERVERS+"/1");
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
	private static void testRequestJsonGetServer()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get single server info, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_SERVER+"/1");
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
    private static void testRequestJsonPutServer()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP PUT request to save server info, use JSON, expect RESPONSE");
        PutMethod post = new PutMethod(getTestHost()+ACTION_SAVESERVER+"/0");
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	/*
        	 * {"id":0,"hostUrl":"testUrl","hostServer":"testServer","name":"testName","isDailyRun":false,"runSchedule":"123456","user":"testUser","password":"testPw"}
        	 */
        	//post.setRequestEntity(new StringRequestEntity(" {\"Server\":[{\"id\":0,\"name\":\"E1F\",\"type\":1}]}"));
        	post.setRequestEntity(new StringRequestEntity(" {\"Server\":[{\"id\":0,\"hostUrl\":\"testUrl\",\"hostServer\":\"testServer\",\"name\":\"testName\",\"isDailyRun\":false,\"runSchedule\":\"123456\",\"user\":\"testUser\",\"password\":\"testPw\"}]}"));

            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();            
        }
    }

    private static void testRequestJsonRemoveServer()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP POST request to remove server, use JSON, expect RESPONSE");
        DeleteMethod post = new DeleteMethod(getTestHost()+ACTION_REMOVESERVER+"/6");
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	
            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();            
        }
    }
}
